<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Token测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"], input[type="password"], textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            margin-right: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            white-space: pre-wrap;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Token测试页面</h1>
        
        <div class="form-group">
            <label for="phone">手机号:</label>
            <input type="text" id="phone" placeholder="请输入手机号" value="13800138000">
        </div>
        
        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" id="password" placeholder="请输入密码" value="123456">
        </div>
        
        <button onclick="login()">登录获取Token</button>
        <button onclick="validateToken()">验证Token</button>
        <button onclick="clearToken()">清除Token</button>
        
        <div class="form-group">
            <label for="tokenDisplay">当前Token:</label>
            <textarea id="tokenDisplay" readonly placeholder="Token将显示在这里"></textarea>
        </div>
        
        <div id="result"></div>
    </div>

    <script>
        // 登录获取Token
        async function login() {
            const phone = document.getElementById('phone').value;
            const password = document.getElementById('password').value;
            
            if (!phone || !password) {
                showResult('请输入手机号和密码', 'error');
                return;
            }
            
            try {
                const response = await fetch('/drone/register/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        phone: phone,
                        password: password
                    })
                });
                
                const result = await response.json();
                
                if (result.code === 200) {
                    localStorage.setItem('token', result.data);
                    document.getElementById('tokenDisplay').value = result.data;
                    showResult('登录成功！\nToken: ' + result.data, 'success');
                } else {
                    showResult('登录失败: ' + result.msg, 'error');
                }
            } catch (error) {
                showResult('登录异常: ' + error.message, 'error');
            }
        }
        
        // 验证Token
        async function validateToken() {
            const token = localStorage.getItem('token');
            
            if (!token) {
                showResult('没有找到Token，请先登录', 'error');
                return;
            }
            
            try {
                const response = await fetch('/drone/register/validateToken', {
                    method: 'GET',
                    headers: {
                        'Authorization': 'Bearer ' + token
                    }
                });
                
                const result = await response.json();
                
                if (result.code === 200) {
                    showResult('Token验证成功！\n用户信息: ' + JSON.stringify(result.data, null, 2), 'success');
                } else {
                    showResult('Token验证失败: ' + result.msg, 'error');
                }
            } catch (error) {
                showResult('Token验证异常: ' + error.message, 'error');
            }
        }
        
        // 清除Token
        function clearToken() {
            localStorage.removeItem('token');
            document.getElementById('tokenDisplay').value = '';
            showResult('Token已清除', 'info');
        }
        
        // 显示结果
        function showResult(message, type) {
            const resultDiv = document.getElementById('result');
            resultDiv.textContent = message;
            resultDiv.className = 'result ' + type;
        }
        
        // 页面加载时显示当前Token
        window.onload = function() {
            const token = localStorage.getItem('token');
            if (token) {
                document.getElementById('tokenDisplay').value = token;
            }
        };
    </script>
</body>
</html>
